﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using Osobe;
using SkolskaUstanova;
using MolbeIPotvrde;

namespace ElektronskiDnevnik
{
    public partial class EvidencijaPrisustva : Form
    {
        int odabraniPredmet;
        int odabraniRazred;
        MySqlConnection conn;
        MySqlDataReader rdr = null;
        public EvidencijaPrisustva(int predmet, int razred, MySqlConnection c)
        {
            InitializeComponent();
            odabraniPredmet = predmet;
            odabraniRazred = razred;
            conn = c;
            
        }

        private void Form9_Load(object sender, EventArgs e)
        {
            MySqlCommand cmd = new MySqlCommand("SELECT * FROM Ucenik WHERE Razred = " + odabraniRazred + ";", conn);
            try
            {
                rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    CheckBox checkBox = new CheckBox();
                    checkBox.Font = new System.Drawing.Font("Segoe UI", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
                    checkBox.ForeColor = System.Drawing.Color.Salmon;
                    checkBox.AutoSize = true;
                    checkBox.Text = "          " + Convert.ToString(rdr["JMBG"]) + "        " + Convert.ToString(rdr["Ime"]) + " " + Convert.ToString(rdr["Prezime"]);
                    flowLayoutPanel1.Controls.Add(checkBox);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Greska", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (rdr != null)
                    rdr.Close();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            MySqlCommand cmd;
            foreach (Control c in flowLayoutPanel1.Controls)
            {
                String select = ((CheckBox)(c)).Text;
                String odabraniUcenik = select.Substring(10, 13);
                int ID_up = 0;
                MySqlCommand cmd1 = new MySqlCommand("SELECT * FROM UcenikPredmet WHERE Ucenik = '" + odabraniUcenik + "' AND Predmet = "+ odabraniPredmet +";", conn);
                try
                 {
                      rdr = cmd1.ExecuteReader();
                      while (rdr.Read())
                           ID_up = Convert.ToInt32(rdr["ID"]);
                       rdr.Close();
                       if (((CheckBox)(c)).Checked)
                            cmd = new MySqlCommand("INSERT INTO Prisustvo (" + "Datum, Prisutan, ID_up" + ") VALUES('" + Convert.ToString((Convert.ToDateTime(dateTimePicker1.Text)).Year) + "-" + Convert.ToString((Convert.ToDateTime(dateTimePicker1.Text)).Month) + "-" + Convert.ToString((Convert.ToDateTime(dateTimePicker1.Text)).Day) + "', " + 1 + ", " + ID_up + ");", conn);
                       else
                             cmd = new MySqlCommand("INSERT INTO Prisustvo (" + "Datum, Prisutan, ID_up" + ") VALUES('" + Convert.ToString((Convert.ToDateTime(dateTimePicker1.Text)).Year) + "-" + Convert.ToString((Convert.ToDateTime(dateTimePicker1.Text)).Month) + "-" + Convert.ToString((Convert.ToDateTime(dateTimePicker1.Text)).Day) + "', " + 0 + ", " + ID_up + ");", conn);
                        cmd.ExecuteNonQuery();
                  }
                  catch (Exception ex)
                   {
                       MessageBox.Show(ex.Message, "Greska", MessageBoxButtons.OK, MessageBoxIcon.Error);
                   }
                   finally
                   {
                       if (rdr != null)
                           rdr.Close();
                   }
            }
        }

      
    }
}
